package com.lebang.controller.ybdapi;

import com.lebang.common.MemberLock;
import com.lebang.common.Require;
import com.lebang.domain.ybd.YbdCategoryConfig;
import com.lebang.domain.ybd.YbdCategoryMemberRecord;
import com.lebang.entity.RequestHeaders;
import com.lebang.model.cences.YbdCategory;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.PointService;
import com.lebang.service.ybd.CategoryConfigService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @Author: hwq
 * @Date: 2021/1/17 10:58
 * @Version 1.0
 */
@RestController
@RequestMapping("/ybd/api/category")
public class AppCategoryDoApi {
    @Autowired
    private CategoryConfigService categoryConfigService;
    @Autowired
    private PointService pointService;
    @Require
    @PostMapping("/info")
    public JsonData info(HttpServletRequest request, String category) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        YbdCategory type = YbdCategory.valueOf(category);
        if (type == null) {
            return Callback.fail("success", "玩法不存在", null);
        }
        return Callback.success("success", "查询成功", categoryConfigService.selectByCategory(type, requestHeaders));
    }

    @Require
    @PostMapping("/pull")
    @MemberLock
    public JsonData ppiull(HttpServletRequest request, String configId) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        YbdCategoryConfig ybdCategoryConfig = categoryConfigService.selectByConfigId(configId);
        if (ybdCategoryConfig == null) {
            return Callback.fail("empty", "201非法参数");
        }
        boolean is = categoryConfigService.checkDayHas(requestHeaders.getUserid(), ybdCategoryConfig.getCategory(), configId, LocalDate.now());
        if (!is) {
            return Callback.fail("empty", "任务已完成");
        }
        LocalDate date = LocalDate.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime star = LocalDateTime.of(date, LocalTime.parse(ybdCategoryConfig.getStarTime()));
        LocalDateTime end = LocalDateTime.of(date, LocalTime.parse(ybdCategoryConfig.getEndTime()));
//        if (localDateTime.isBefore(star) || localDateTime.isAfter(end)) {
//            return Callback.fail("empty", "超出时间了");
//        }
        categoryConfigService.createRecord(ybdCategoryConfig, requestHeaders);
        return Callback.success("success", "查询成功");
    }
    @Require
    @PostMapping("/push")
    @MemberLock
    public JsonData push(HttpServletRequest request, Integer recordId) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        YbdCategoryMemberRecord record = categoryConfigService.selectByRecordId(recordId);
        if (record == null) {
            return Callback.fail("empty", "201非法参数");
        }
        if (!record.getStatus().equals(0)) {
            return Callback.fail("empty1", "202非法参数");
        }
        if (!record.getMemberId().equals(requestHeaders.getUserid())) {
            return Callback.fail("empty2", "203非法参数");
        }
        if (!record.getImei().equals(requestHeaders.getImei())) {
            return Callback.fail("empty3", "204非法参数");
        }
        if(!LocalDateTime.now().isAfter(record.getPushTime())){
            return Callback.fail("empty4", "体验时间未到");
        }
        categoryConfigService.pushRecord(record.getId(), 1);
        pointService.addPoint(record.getMemberId(),record.getPoint(), TaskCategory.valueOf(record.getCategory()),requestHeaders,"");
        return Callback.success("success", "查询成功");
    }


    @PostMapping("/history")
    public JsonData history(HttpServletRequest request) {
        categoryConfigService.selectAllCategory();

        return Callback.success("success", "查询成功", null);
    }
}
